<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jun 16, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RCoreImporter::cls( "helper.RHtmlHelper" );

/**
 * Class keeping all resoucrce of a site
 *
 */
class SiteResourceBase extends Object {
	
	/**
	 * Application
	 *
	 * @var string
	 */
	var $application;
	
	/**
	 * Action
	 *
	 * @var string
	 */
	var $action;
	
	/**
	 * Task
	 *
	 * @var string
	 */
	var $task;
	
	/**
	 * Option
	 *
	 * @var string
	 */
	var $option;
	
	/**
	 * Extend more extend for a resource
	 *
	 * @var array
	 */
	var $_extend;
	
	var $_mapRequest = array ("application" => "app", "action" => "act", "task" => "task", "option" => "option" );
	
	/**
	 * Constructor
	 * 
	 * @param string $application
	 * @param string $action
	 * @param string $task
	 * @param string $option
	 * @return SiteResource
	 */
	function __construct($application = null, $action = null, $task = null, $option = null) {
		$this->application = $application;
		$this->action = $action;
		$this->task = $task;
		$this->option = $option;
		$this->_extend = array ();
	}
	
	/**
	 * Set value for this resource from a request or an array have the same struct.
	 *
	 * @param array $arrayRequest this can be $_POST, $_GET, $_REQUEST
	 */
	function setValueFromRequest($arrayRequest, $withExt = false) {
		if (!is_array($arrayRequest)) {
			return;
		}
		$size = count ( $this->_arrProperties );
		foreach ( $this->_mapRequest as $proName => $key ) {
			if (array_key_exists ( $key, $arrayRequest )) {
				$this->$proName = $arrayRequest [$key];
			}
		}
		
		if ($withExt) {
			foreach ( $arrayRequest as $key => $value ) {
				if (in_array ( $key, $this->_mapRequest )) {
					continue;
				}
				$this->_extend [$key] = $value;
			}
		}
	}
	
	/**
	 * Set value for extend
	 *
	 * @param string $key key name
	 * @param string $value
	 */
	function setExtend($key, $value) {
		foreach ( $this->_mapRequest as $prop => $keyWord ) {
			if ($key == $keyWord) {
				$this->$prop = $value;
				return;
			}
		}
		
		$this->_extend [$key] = $value;
	}
	
	/**
	 * Get extend
	 *
	 * @param string $key
	 * @return string
	 */
	function getExtend($key) {
		return $this->_extend [$key];
	}
	
	/**
	 * Remove Extend
	 *
	 * @param string $key
	 */
	function removeExtend($key) {
		unset ( $this->_extend [$key] );
	}
	
	/**
	 * Get the link of this resource
	 *
	 */
	function getLink($withIndex = true, $withExtend = true) {
		if ($withIndex) {
			$link = "index.php?";
		}
		$arrLink = array ();
		$size = count ( $this->_arrProperties );
		foreach ( $this->_mapRequest as $proName => $key ) {
			$val = $this->$proName;
			if (! empty ( $val )) {
				$val = urlencode ( $val );
				$arrLink [] = "{$key}={$val}";
			}
		}
		
		if ($withExtend) {
			foreach ( $this->_extend as $key => $value ) {
				$val = urlencode ( $value );
				$arrLink [] = "{$key}={$val}";
			}
		}
		$link .= implode ( "&", $arrLink );
		return $link;
	}
	
	/**
	 * Create the HTML form with hidden fields to keep all infomation of current resource
	 *
	 * @param string $formName
	 * @param string $method
	 * @param string $extAttributes
	 * @return string
	 */
	function createHtmlFormWithHidden($formName, $withExtend = true, $method = "post", $extAttributes = "") {
		$result = "<form name=\"$formName\" id=\"$formName\" action=\"\" method=\"$method\" {$extAttributes}> \n";
		$result .= $this->createHtmlHiddenFields ( $withExtend );
		$result .= "</form>";
		return $result;
	}
	
	/**
	 * Create the HTML hidden fields to keep all infomation of current resource
	 *
	 * @return string
	 */
	function createHtmlHiddenFields($withExtend = true, $extIdSub = "") {
		$result = "";
		
		$size = count ( $this->_arrProperties );
		foreach ( $this->_mapRequest as $proName => $key ) {
			$result .= RHtmlHelper::createHiddenHtml ( "prev_{$key}", $this->$proName ) . "\n";
			$result .= RHtmlHelper::createHiddenHtml ( $key, $this->$proName ) . "\n";
		}
		
		if ($withExtend) {
			foreach ( $this->_extend as $key => $val ) {
				$result .= RHtmlHelper::createHiddenHtml ( $key, $val, $key . $extIdSub ) . "\n";
			}
		}
		
		return $result;
	}
	
	/**
	 * Check if the input Resource is the sub resource of this.
	 * Ex: 	this (app=>'news', act=>'category', task='edit');
	 * 		input (app=>'news');
	 *   ==> return true
	 * 
	 * @param SiteResource $parentResource
	 * 
	 * @return boolean
	 */
	function checkCover($parentResource) {
		if (empty ( $this->application ) && empty ( $parentResource->application )) {
			return true;
		}
		
		$arrProperties = $this->_arrProperties;
		$size = count ( $arrProperties );
		foreach ( $this->_mapRequest as $propertie => $key ) {
			$val = $this->$propertie;
			$subVal = $parentResource->$propertie;
			
			if ($val != $subVal) {
				if (empty ( $subVal )) {
					return true;
				}
				return false;
			}
		}
		
		return true;
	}
	
	/**
	 * Clone current object
	 *
	 * @return SiteResource new object
	 */
	function cloneObject() {
		$newObject = new SiteResource ( $this->application, $this->action, $this->task, $this->option );
		
		foreach ( $this->_extend as $key => $val ) {
			$newObject->_extend [$key] = $val;
		}
		
		return $newObject;
	}
	
	/**
	 * This function will remove all extend that don't have prefix like the input
	 *
	 * @param string $prefix
	 */
	function removeExtendWithoutPrefix($prefix = "x_") {
		foreach ( $this->_extend as $key => $val ) {
			if ($key == 'lang') {
				continue;
			}
			if (strpos ( $key, $prefix ) === 0) {
				continue;
			}
			unset ( $this->_extend [$key] );
		}
	}
	
	/**
	 * Get the hash value of current object
	 */
	function hash() {
		$hash = $this->getLink ();
		return md5 ( $hash );
	}
	
	/**
	 * Get the value of any REQUEST value inside this site resource
	 *
	 * @param string $key
	 * @return mixed
	 */
	function getValueOf($key) {
		if (key_exists($key, $this->_extend)) {
			return $this->_extend[$key];
		}
		foreach ($this->_mapRequest as $prop => $k) {
			if ($k == $key) {
				return $this->$prop;
			}
		}
		return null;
	}
}
?>
